import { isArray, isNumber } from "lodash";
import * as plans from "./plan";
import * as local_courses from "./course";
import { Point, TRAIN_ALL_DONE } from "./constant";
// 根据计划名称获取计划
export const getPlanByTitle = (title) => {
    return plans[title];
};

//根据训练点获取课程
export const getCourseByTrainPoint = (train_point) => {
    // console.log("train_point--", train_point);

    let plan = getPlanByTitle(train_point.plan);
    // console.log("plan---", plan);

    //根据month, week, day, 获取第一天的课程
    const month = train_point.month;
    const week = train_point.week;
    const day = train_point.day;
    // console.log("local_courses---",local_courses);
    // 根据计划名称, 获取训练中的课程名称
    let training_courses = local_courses[train_point.plan];
    // console.log("training_courses---", training_courses);

    // console.log("training_courses--",  training_courses);
    let today_courseS: any[] = [];

    for (let p of plan) {
        if (p.title == month) {
            let plans = p.plans;
            for (let month_plan of plans) {
                // 根据月计划, 和week,  获取到周计划
                // console.log("month_plan---", month_plan);
                if (month_plan.week == week) {
                    // console.log("月计划000",month_plan)
                    for (let week_plan of month_plan.plans) {
                        // 获取周计划
                        if (week_plan.day == day) {
                            let course = week_plan.course;
                            if (isNumber(course)) {
                                // console.log("course---", course);
                                // 根据课程下标获取课程

                                let today_course = getCourseByIdx(training_courses, course);
                                // console.log("计划---", course);
                                today_courseS.push(today_course);
                                return today_courseS;
                            } else if (isArray(course)) {
                                for (let c of course) {
                                    // 根据课程下标获取课程
                                    let today_course = getCourseByIdx(training_courses, c);
                                    today_courseS.push(today_course);
                                }
                                return today_courseS;
                            }

                        }
                    }
                }
            }
        }
    }
    // 未查询到训练时, 返回空数组
    return today_courseS;
};

/**
 * 
 * @param training_courses 当前正在训练的课程
 * @param idx 课程的下标
 */
export const getCourseByIdx = (training_courses, idx) => {
    // console.log("training_courses---",  training_courses);
    // console.log("idx---",  idx);
    for (let t of training_courses) {
        // console.log("t---", t);
        if (t.idx == idx) {
            return t;
        }
    }
};


//根据上一个训练内容, 获取下一个训练可能
export const getNextPointByLast = async (last_point) => {
    const { plan, day, month, week } = last_point;
    let new_point: Point = {
        plan: plan,
        week: '',
        month: '',
        day: ''
    };
    let all_points = getAllPointsByPlanName(plan);
    // console.log("all_points---", all_points);
    let length = all_points.length;

    let res :any;
    all_points.map( (point, idx)=>{
        // console.log("point---", point);
        if(point['month'] == month && point['week'] == week && point['day'] == day ){
            if(idx < length - 1){
                // 如果今天不是本计划的最后一天, 返回新的训练计划
                let new_idx = idx + 1;
                res = all_points[new_idx];
                console.log("res---", res);
                
            }else{
                // console.log("今天是最后一天了");
                res = TRAIN_ALL_DONE;
            }
        }
    })
    return res;
};

// 根据计划名称, 获取一维化之后的训练计划
export const getAllPointsByPlanName = (plan_name : String)=>{
    let plans = getPlanByTitle(plan_name);
    let all_points: Point[] = [];
    plans.map((month_data) => {
        // console.log("month_data---", month_data);
        month_data['plans'].map((week_data) => {
            // console.log("week_data_plans---", week_data['plans']);
            week_data['plans'].map((day_data) => {
                // console.log("day_data---", day_data);
                let point_item: Point = {
                    plan: plan_name,
                    week:week_data['week'],
                    month: month_data['title'],
                    day: day_data['day']
                };
                all_points.push(point_item);
            });

        });
    });
    return all_points;
}

//根据训练点训练进度
export const getTrainProgressByPoint = (point : Point)=>{
    const { plan ,day, month, week } = point;
    const all_points = getAllPointsByPlanName(plan);
    let length = all_points.length;
    let current_idx = 0;
    all_points.map( (point, idx)=>{
        // console.log("point---", point);
        if(point['month'] == month && point['week'] == week && point['day'] == day ){
            current_idx = idx
        }
    })

    let p = (current_idx/length).toFixed(2);
    return p;
}